<?php

namespace App\Http\Controllers\Web;

use App\Http\Controllers\Controller;

use App\Models\Baoming;
use App\Models\Popularity;
use App\Models\Singer;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Validator;


class IndexController extends Controller
{
    protected function validator($data, $rules, $messages = [])
    {


        $validator = Validator::make($data, $rules, $messages);

        if ($validator->fails()) {

            foreach ($validator->messages()->all() as $message) {

                echo "<script>
alert('{$message}');
history.go(-1);
</script>";
                exit ();
            }
        }
        return $data;
    }

    public function index()
    {
        $data = [];

        return view('web.home');
    }

    public function getAbout()
    {
        return view('web.about');
    }

    public function getBaoming()
    {
        $params = Input::all();

        if (isset($params['team'])) {
            return view('web.enter-team');
        }

        return view('web.enter-personal');
    }

    public function getHot()
    {
        $Popularity = new Popularity();

        $qishu = $Popularity->max('qishu');

        $list = $Popularity->with('singer')->where('qishu', $qishu)->get()->toArray();

        $old = $Popularity->with('singer')->where('qishu', $qishu - 1)->get()->toArray();

        return view('web.hot', compact('list', 'old'));
    }

    public function getJuesai()
    {
        $singer = new Singer();

        $list = $singer->where('final',1)->get()->toArray();


        return view('web.hot-12', compact('list'));
    }


    public function postBaoming()
    {

        $params = Input::all();


        $rules = [
            'relname' => 'required',
            'song' => 'required',
            'email' => 'required|email',
            'mobile' => 'required',
        ];


        $messages = [
            'relname.required' => '参赛者姓名不能为空',
            'song.required' => '参赛曲目不能为空',
            'email.required' => '邮箱不能为空',
            'mobile.required' => '联系方式不能为空',
        ];
        self::validator($params, $rules, $messages);


        $data = [
            'type' => isset($params['type']) ? $params['type'] : 1,
            'relname' => isset($params['relname']) ? $params['relname'] : '',
            'sex' => isset($params['sex']) ? $params['sex'] : 1,
            'age' => isset($params['age']) ? $params['age'] : 0,
            'yueqi' => '木吉他',
            'relname2' => isset($params['relname2']) ? $params['relname2'] : '',
            'sex2' => isset($params['sex2']) ? $params['sex2'] : 0,
            'age2' => isset($params['age2']) ? $params['age2'] : 0,
            'yueqi2' => isset($params['yueqi2']) ? $params['yueqi2'] : '',
            'song' => isset($params['song']) ? $params['song'] : '',
            'yuanchuang' => isset($params['yuanchuang']) ? $params['yuanchuang'] : 0,
            'video' => isset($params['video']) ? $params['video'] : '',
            'email' => isset($params['email']) ? $params['email'] : '',
            'mobile' => isset($params['mobile']) ? $params['mobile'] : '',
        ];

        $model = new Baoming();
        $rt = $model->insert($data);

        if ($rt) {

            if(!isset($params['isMobile'])){
                return redirect('/action/success');
            }else{
                return redirect('/mobile/action/success');
            }

        }
    }


    public function getSuccess()
    {
        return view('web.success');
    }

}
